package org.linlinjava.litemall.db.service;

import java.util.List;

import javax.annotation.Resource;

import org.beetl.sql.core.query.LambdaQuery;
import org.linlinjava.litemall.db.domain.LitemallRegion;
import org.linlinjava.litemall.db.mapper.LitemallRegionDao;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

@Service
public class LitemallRegionService {

    @Resource
    private LitemallRegionDao regionMapper;

    public List<LitemallRegion> getAll(){
    	LambdaQuery<LitemallRegion> query = regionMapper.createLambdaQuery();
    	
    	byte b = 4;
    	query.andNotEq(LitemallRegion::getType, b);
    	return query.select();
		/*
		 * LitemallRegionExample example = new LitemallRegionExample(); byte b = 4;
		 * example.or().andTypeNotEqualTo(b); return
		 * regionMapper.selectByExample(example);
		 */
    }

    public List<LitemallRegion> queryByPid(Integer parentId) {
    	LambdaQuery<LitemallRegion> query = regionMapper.createLambdaQuery();
    	query.andEq(LitemallRegion::getPid, parentId);
    	return query.select();
		/*
		 * LitemallRegionExample example = new LitemallRegionExample();
		 * example.or().andPidEqualTo(parentId); return
		 * regionMapper.selectByExample(example);
		 */
    }

    public LitemallRegion findById(Integer id) {
    	return regionMapper.single(id);
        //return regionMapper.selectByPrimaryKey(id);
    }

    public List<LitemallRegion> querySelective(String name, Integer code, Integer page, Integer size, String sort, String order) {
    	
    	
    	LambdaQuery<LitemallRegion> query = regionMapper.createLambdaQuery();
    	

        if (!StringUtils.isEmpty(name)) {
        	query.andLike(LitemallRegion::getName, "%" + name + "%");
        }
        if (!StringUtils.isEmpty(code)) {
        	query.andEq(LitemallRegion::getCode, code);
        }

        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
        	query.orderBy(sort + " " + order);
        }
        
        query.limit(page, size);
        return query.select();
    	
		/*
		 * LitemallRegionExample example = new LitemallRegionExample();
		 * LitemallRegionExample.Criteria criteria = example.createCriteria();
		 * 
		 * if (!StringUtils.isEmpty(name)) { criteria.andNameLike("%" + name + "%"); }
		 * if (!StringUtils.isEmpty(code)) { criteria.andCodeEqualTo(code); }
		 * 
		 * if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
		 * example.setOrderByClause(sort + " " + order); }
		 * 
		 * PageHelper.startPage(page, size); return
		 * regionMapper.selectByExample(example);
		 */
    }

}
